package com.weiaibenpao.demo.chislim.ui.activity.running;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.base.BaseActivity;
import com.weiaibenpao.demo.chislim.bean.ChinaRecordBean;
import com.weiaibenpao.demo.chislim.db.preferences.UserPreferences;
import com.weiaibenpao.demo.chislim.presenter.ChinaRecordPresenter;
import com.weiaibenpao.demo.chislim.ui.adapter.RecordAdapter;
import com.weiaibenpao.demo.chislim.utils.Constant;
import com.weiaibenpao.demo.chislim.view.ChinaRecordView;

import butterknife.BindView;
import butterknife.OnClick;

public class ChinaRunRecordActivity extends BaseActivity<ChinaRecordView, ChinaRecordPresenter> implements ChinaRecordView ,BaseQuickAdapter.RequestLoadMoreListener{


    @BindView(R.id.rank_recycler_view)
    RecyclerView rankRecyclerView;
    private RecordAdapter recordAdapter;
    private int page=1;
    private int userId;
    private View emptyView;

    @Override
    public int setLayoutResourceID() {
        return R.layout.activity_china_run_record;
    }

    @Override
    public void init(Bundle savedInstanceState) {
         userId = UserPreferences.getInstance().getInt(UserPreferences.USER_ID);
        initAdapter();
        doRequest();
    }

    private void doRequest(){
        mPresenter.getRecord(userId, page, Constant.DEFAULT_PAGE_SIZE);

    }
    private void initAdapter() {
        emptyView = LayoutInflater.from(this).inflate(R.layout.layout_empty, null);
        ((TextView) emptyView.findViewById(R.id.tv_empty_new)).setText("这个人很懒，没有留下任何足迹！");
        recordAdapter = new RecordAdapter(R.layout.record_item_layout);
        rankRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        rankRecyclerView.setAdapter(recordAdapter);
    }

    public static void startNewActivity(Context context) {
        Intent intent = new Intent(context, ChinaRunRecordActivity.class);
        context.startActivity(intent);
    }

    @Override
    public ChinaRecordPresenter initPresenter() {
        return new ChinaRecordPresenter();
    }

    @Override
    public void requestSuccess(ChinaRecordBean chinaRecordBean) {
       recordAdapter.setEmptyView(emptyView);
        recordAdapter.addData(chinaRecordBean.getList());

        if (chinaRecordBean.getList().size() < Constant.DEFAULT_PAGE_SIZE) {
            recordAdapter.loadMoreEnd();
        } else {
            recordAdapter.loadMoreComplete();
        }

    }



    @OnClick(R.id.left_image)
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.left_image:
                finish();
                break;
        }
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        doRequest();
    }
}
